<template>
  <div id="container">
    <el-table :data="bookList" style="width: 100%">
      <el-table-column label="图书ID" width="180" prop="id" align="center"/>
      <el-table-column label="图书名称"  prop="title"  align="center"/>
      <el-table-column label="作者"  prop="author"  align="center"/>
      <el-table-column label="出版社"  prop="publisher"  align="center"/>
      <el-table-column label="发行年份"  prop="publicationYear"  align="center"/>
      <el-table-column label="图书国标码"  prop="isbn"  align="center"/>
      <el-table-column label="图书封面"   align="center">
        <template #default="scope">
          <el-image v-if="scope.row.coverUrl" :src="scope.row.coverUrl" alt="" width="100px" />
          <!--<img :src="scope.row.coverUrl" alt="" width="100px" />-->
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="center">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>

</template>
<script setup>
import {listBooks} from "@/api/books/books";
const { proxy } = getCurrentInstance()
const total = ref(0)
const loading = ref(true)
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  // rules: {
  //   name: [
  //     { required: true, message: "班级名称不能为空", trigger: "blur" }
  //   ],
  //   grade: [
  //     { required: true, message: "年级不能为空", trigger: "blur" }
  //   ],
  // }
})
// 将data转换为ref对象
const { queryParams } = toRefs(data)

// 1. 图书列表
const bookList  = ref([])
// 2. 编辑图书
const handleEdit = (index, row) => {
  console.log(index, row)
}
// 3. 删除图书
const handleDelete = (index, row) => {
  console.log(index, row)
}
// 4. 开始查询所有图书
function getList() {
  loading.value = true
  listBooks(queryParams.value).then(res => {
    console.log("res:",res)
    loading.value = false
    bookList.value = res.rows
    total.value = res.total
  })
}
getList()
</script>
<style scoped lang="scss">
  #container {
    padding: 10px;
  }
</style>
